package com.qingxin.sys.smslib;

import com.qingxin.sys.support.BaseThread;

public class SmsInDataThread extends BaseThread {

	//唯一实例
	private static SmsInDataThread smsInDataThread;
	
	/**
	 * 获取唯一实例
	 * @return
	 */
	public static SmsInDataThread getInstance(){
		if(smsInDataThread != null && smsInDataThread.isRunning()){
			return smsInDataThread;
		}
		smsInDataThread = new SmsInDataThread();
		return smsInDataThread;
	}
	
	
	/**
	 * 线程是否运行中
	 * @return
	 */
	public static boolean isActive(){
		return smsInDataThread != null && smsInDataThread.isRunning();
	}
	
	
	private SmsInDataThread() {
	}
	
	
	@Override
	public void run() {
		running = true;
		while(running){
			try {
				SmsInDataDeque.tryWait();
				if(!isRunning()){
					continue;
				}
				
				SmsData smsData = SmsInDataDeque.poll();
				if(smsData == null){
					continue;
				}
				
				//接收处理
				//System.out.println(smsData);
				SmsFactory.receivedSmsHandler(smsData);
				
			} catch (Exception e) {
				e.printStackTrace();
				try {
					Thread.sleep(1000);
				} catch (Exception e2) {
				}
			}
		}
		smsInDataThread = null;
		running = false;
		SmsInDataDeque.clear();
	}
	
	
}
